//
//  YDEmotionLeyBoardView.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/18.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit

class YDEmotionLeyBoardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    设置视图
    private func setupUI(){
        
    backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
//        添加控件
        addSubview(emotionBottomView)
        addSubview(emotionPageView)
        addSubview(pageControl)
//        添加约束
        emotionBottomView.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        emotionPageView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(emotionBottomView.snp_top)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(emotionPageView).offset(5)
            make.centerX.equalTo(emotionPageView)
        }
//        底部视图闭包回调
        emotionBottomView.closure = {(type:YDEmotionBottomViewType)->() in
//            switch type{
//            case .Default:
//                print("默认")
//            case .Emoji:
//                print("emoji")
//            case .Lxh:
//                print("浪小花")
//            }
//            获取indexpath
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
//            滚动到制定位置
            self.emotionPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
//      设置pagecontrol
            self.setupPageControl(indexPath)
    
        }
//        设置pageconrol
        setupPageControl(NSIndexPath(forItem: 0, inSection: 0))
        }
        
    //    懒加载控件
    private lazy var emotionBottomView:YDEmotionBottomView = YDEmotionBottomView(frame: CGRectZero)
//    自定义表情键盘
    private lazy var emotionPageView:YDBottomPageView = {
        let view = YDBottomPageView()
        view.backgroundColor = self.backgroundColor
        view.bounces = false
        view.pagingEnabled = true
//        隐藏滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
//        设置代理
        view.delegate = self
        
        return view
    
    }()
    
//    pageControl必须添加在keyboard中 才能在点击按钮和滑动colletionview都能监听的到
    private lazy var pageControl: UIPageControl = {
        let view = UIPageControl()

//        默认颜色
//        view.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
//       给只读属性赋值 xcode6.4以后不再开放 但可以使用
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
//        选中的颜色
//        view.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
//        取消用户交互
        view.userInteractionEnabled  = false
        
        
        return view
    }()
    
}

    
extension YDEmotionLeyBoardView:UICollectionViewDelegate{

    func scrollViewDidScroll(scrollView: UIScrollView) {
//        获取屏幕中心点 相对于(pageView)
        let centerX = KSCREENW*0.5 + scrollView.contentOffset.x
//        获取屏幕显示的cell 系统没有排序  所以需要自己排序
        let cells = self.emotionPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
//        如果屏幕上显示2个cell才会判断
        if cells.count == 2{
//            第一个cell
            let firstCell = cells.first!
//            最后一个cell
            let lastCell = cells.last!
//            定义一个indexpath
            let indexpath:NSIndexPath
//            判断中心点是否在firstcell中
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
//                print("第一个cell显示的多")
                indexpath = emotionPageView.indexPathForCell(firstCell)!
            }else{
//                print("第二个cell显示的多")
                indexpath = emotionPageView.indexPathForCell(lastCell)!
            
            }
            
//        设置选中的按钮 用button的tag 与section关联
            emotionBottomView.setupCurrentButton(indexpath.section)
            setupPageControl(indexpath)
                    }
    }
//    设置pagecontrol
    func  setupPageControl(indexpath:NSIndexPath){
        //        设置总页数
        self.pageControl.numberOfPages = YDEmoticonTools.shareTools.allEmoticons[indexpath.section].count
        //        选中的页数
        self.pageControl.currentPage = indexpath.item

    
    }
}
    


